/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;

public class CekStokBarangPusat {

    private String kode_barang;
    private String nama_barang;
    private String kode_kategori;
    private String jumlah;

    public CekStokBarangPusat() {
    }

    public CekStokBarangPusat(String kode_barang, String nama_barang, String kode_kategori, String jumlah) {
        this.kode_barang = kode_barang;
        this.nama_barang = nama_barang;
        this.kode_kategori = kode_kategori;
        this.jumlah = jumlah;
    }

    public String getKode_barang() {
        return kode_barang;
    }

    public void setKode_barang(String kode_barang) {
        this.kode_barang = kode_barang;
    }

    public String getNama_barang() {
        return nama_barang;
    }

    public void setNama_barang(String nama_barang) {
        this.nama_barang = nama_barang;
    }

    public String getKode_kategori() {
        return kode_kategori;
    }

    public void setKode_kategori(String kode_kategori) {
        this.kode_kategori = kode_kategori;
    }

    public String getJumlah() {
        return jumlah;
    }

    public void setJumlah(String jumlah) {
        this.jumlah = jumlah;
    }

    public ResultSet tampilStokPusat() {
        Connection con = DataBase.Koneksi.open();
        Statement stat = null;
        try {
            stat = con.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(CekStokBarangPusat.class.getName()).log(Level.SEVERE, null, ex);
        }
        String que1 = "select * from STOCKBARANGPUSAT_FROGSTONE order by KODEBARANG asc";
        ResultSet res = null;
        try {
            res = stat.executeQuery(que1);
        } catch (SQLException ex) {
            Logger.getLogger(CekStokBarangPusat.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }

    public ResultSet tampilStokPusatKonfirm(String kode) {
        Connection con = DataBase.Koneksi.open();
        Statement stat = null;
        try {
            stat = con.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(CekStokBarangPusat.class.getName()).log(Level.SEVERE, null, ex);
        }
        String que1 = "select * from STOCKBARANGPUSAT_FROGSTONE where KODEBARANG='" + kode + "'";
        ResultSet res = null;
        try {
            res = stat.executeQuery(que1);
        } catch (SQLException ex) {
            Logger.getLogger(CekStokBarangPusat.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }

    public void tambahStockPusat(CekStokBarangPusat barang) {
        Connection con = DataBase.Koneksi.open();
        Statement stat = null;
        try {
            stat = con.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(CekStokBarangPusat.class.getName()).log(Level.SEVERE, null, ex);
        }
        String que1 = "insert into " + " STOCKBARANGPUSAT_FROGSTONE "
                + "values ('" + kode_barang + "','" + nama_barang + "','" + kode_kategori
                + "','" + jumlah + "')";
        ResultSet res = null;
        try {
            res = stat.executeQuery(que1);
        } catch (SQLException ex) {
            Logger.getLogger(CekStokBarangPusat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String generateKodeBarang(Date det, String id) {
        String kode;
        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
        kode = id + sdf.format(det) + "0";
        return kode;
    }

    public String kodeBarangFix(String kode) throws SQLException {
        String code = null;
        String kod = kode.substring(0, 11);
        int a = 0;
        Connection con = DataBase.Koneksi.open();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select KODEBARANG from STOCKBARANGPUSAT_FROGSTONE where KODEBARANG like '" + kod + "%'");
        while (rs.next()) {
            String sementara = rs.getString("KODEBARANG");
            String s2 = sementara.substring(11);
            a = Integer.parseInt(s2);
            a++;
        }
        return code = kod + String.valueOf(a);
    }

    public void updateStok(String kode, String jum) throws SQLException {
        Connection con = DataBase.Koneksi.open();
        Statement sta = con.createStatement();
        String sql = "update STOCKBARANGPUSAT_FROGSTONE set JUMLAHBARANG='" + jum + "' where KODEBARANG='" + kode + "'";
        sta.executeUpdate(sql);
    }

    public void deleteStokBarangHabis() throws SQLException {
        Connection con = DataBase.Koneksi.open();
        Statement sta = con.createStatement();
        String sql = "delete from STOCKBARANGPUSAT_FROGSTONE where JUMLAHBARANG='0'";
        sta.executeUpdate(sql);
    }
}
